#pragma once

#include <cstdint>
#include <map>
#include <memory>

#include "asio.h"
#include "io-context.h"

#include "http-session.h"


class HttpDispatcher;

class HttpServer {
public:
    HttpServer(IOContext &context, uint16_t port, HttpDispatcher &dispatcher);

    virtual ~HttpServer();

    HttpDispatcher &dispatcher() {
        return dispatcher_;
    }

    void remove(HttpSession *ptr);

protected:
    virtual std::unique_ptr<HttpSession> createSession(tcp::socket &&socket) = 0;

    void start();

private:
    void asyncAccept();

private:
    tcp::acceptor acceptor_;
    HttpDispatcher &dispatcher_;
    std::map<HttpSession *, std::unique_ptr<HttpSession>> connections_;
};
